﻿using System;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Xml.Linq;
using DocumentFormat.OpenXml.Wordprocessing;

namespace converter.template
{
    public enum BorderStyle
    {
        Default, // Treated as None
        None,
        Dotted,
        Dashed,
        Solid,
        Double,
        DashDot,
        DashDotDot,
    }

    public sealed class Border
    {
        // Fields

        //private Color color;
        private string color;
        private BorderStyle style;
        private Size width;

        // Constructors

        // Creates an instance with the default values.
        public Border()
        {
            //color = Color.Black;
            color = "auto";
            style = BorderStyle.Default;
            width = new Size("1pt");
        }

        // Creates an instance of the Border with the values found in the style element.
        // The name tells what kind of border should be read. Possible values are
        // Border, TopBorder, BottomBorder, LeftBorder, RightBorder.
        public Border(XElement style, string name)
            : this()
        {
            Debug.Assert(style != null, "The style argument can not be null.");

            XElement border = style.Elements().Where(x => x.Name.LocalName == name).SingleOrDefault();
            if (border == null)
                return; // No element, stop processing.

            XElement e;

            // Border color
            e = border.Elements().Where(x => x.Name.LocalName == "Color").SingleOrDefault();
            if (e != null)
            {
                if (e.Value.StartsWith("#"))
                    color = e.Value.Substring(1);
                else
                    color = e.Value;
            }
                //color = ReportColor.FromString(e.Value);

            // Border style
            e = border.Elements().Where(x => x.Name.LocalName == "Style").SingleOrDefault();
            if (e != null)
                this.style = (BorderStyle)Enum.Parse(typeof(BorderStyle), e.Value, true);

            if (color != "auto" && this.style <= BorderStyle.None)
                this.style = BorderStyle.Solid;

            // Border width
            e = border.Elements().Where(x => x.Name.LocalName == "Width").SingleOrDefault();
            if (e != null)
                width = new Size(e.Value);
        }

        // Accessors

        // Gets the border's style.
        public BorderStyle Style
        {
            get { return style; }
        }

        // Gets the border's width.
        public Size Width
        {
            get { return width; }
        }

        // Gets the border's color.
        public string Color
        {
            get { return color; }
        }

        // Methods

        // Returns the current BorderStyle converted to BorderValues.
        public BorderValues ToBorderValues()
        {
            string value = style.ToString();
            if (Enum.IsDefined(typeof(BorderValues), value))
                return (BorderValues)Enum.Parse(typeof(BorderValues), value);
            else if (style == BorderStyle.Solid)
                return BorderValues.Single;
            else
                return BorderValues.None;
        }
    }
}
